/*
 * Ext JS Library 2.2
 * Copyright(c) 2006-2008, Ext JS, LLC.
 * licensing@extjs.com
 * 
 * http://extjs.com/license
 */

Ext.onReady(function(){
		
		Ext.QuickTips.init();

    // turn on validation errors beside the field globally
    	Ext.form.Field.prototype.msgTarget = 'side';


		var ds = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({
            url: 'getreview'
        }),
        reader: new Ext.data.JsonReader({
            root: 'papers',
            totalProperty: 'totalCount',
            id: 'id'
        }, [
        		{name: 'id'},
        		{name: 'title'},
        		{name: 'date_sent', type: 'date', dateFormat: 'Y-m-d G:i:s'},
            	{name: 'file_pdf'},            
            	{name: 'file_doc'},
				{name: 'status_id'},
				{name: 'paper_status'},
				{name: 'reviews_count'},
				{name: 'assignment_id'},
				{name: 'abstract'},
				{name: 'area_name_' + lang},
				{name: 'category_name_' + lang}
        ])
    });
	function renderReview(value, p, r){
		if(value == '0'){
			return String.format('<a href="../review/?assignment_id={0}&paper_id={1}">' + lbl_review + '</a><img src="../../images/{2}.jpg">', r.data['assignment_id'], r.id, 0);
		}else{
			return String.format('<a href="../review/?assignment_id={0}&paper_id={1}&edit=1">' + lbl_edit_review + '</a><img src="../../images/{2}.jpg">', r.data['assignment_id'], r.id, 2);
		}
    }
	
	function renderDownload(value, p, r){
		var str = '';
		var img_pdf = '<img src="../../images/pdf.png">&nbsp;';
		var img_doc = '<img src="../../images/doc.png">&nbsp;';
		var pdf = r.data['file_pdf']
		var doc = r.data['file_doc']
		if(pdf != ''){
			str += String.format('<a href="../paper/download/?id={0}&ft=pdf">{2}</a><a href="../paper/download/?id={0}&ft=pdf">{1}</a>', value, pdf, img_pdf);
		}
		if(doc != ''){
			str += String.format('<br/><a href="../paper/download/?id={0}&ft=doc">{2}</a><a href="../paper/download/?id={0}&ft=doc">{1}</a>', value, doc, img_doc);
		}
		return str;
    }
	
	function renderStatus(value, p, r){
		return String.format('<img src="../../images/{0}.jpg" alt="{1}">', value, r.data['paper_status']);
    }
    
    // create the grid
    var grid = new Ext.grid.GridPanel({
        store: ds,
        columns: [
            {header: lbl_title, width: 150, dataIndex: 'title', sortable: true},
            {header: lbl_date_sent, width: 130, dataIndex: 'date_sent', sortable: true, renderer: Ext.util.Format.dateRenderer('d/m/Y G:i:s')},
			{header: lbl_files, width: 120, dataIndex: 'id', sortable: false, renderer: renderDownload},
			{header: lbl_review_paper, width: 150, dataIndex: 'reviews_count', sortable: true, renderer: renderReview}
        ],
		sm: new Ext.grid.RowSelectionModel({singleSelect: true}),
		viewConfig: {
			forceFit: true
		},
		split: true,
		region: 'north',		
        width:550,
        height:200,
        loadMask:true
    });
	
    var download_pdf;
	var download_doc;

	var ct = new Ext.Panel({
		renderTo: 'assigned-grid',
		frame: true,
		title: lbl_panel_title,
		width: 550,
		height: 400,
		layout: 'border',
		items: [
			grid,
			{
				id: 'detailPanel',
				region: 'center',
				bodyStyle: {
					background: '#ffffff',
					padding: '7px'
				},
				html: lbl_details
			}
		]
	})
	grid.getSelectionModel().on('rowselect', function(sm, rowIdx, r) {
		var detailPanel = Ext.getCmp('detailPanel');
		if(r.data.file_pdf != ''){
			download_pdf = '<a href="../paper/download/?id={id}&ft=pdf"><img src="../../images/pdf.png"></a><a href="../paper/download/?id={id}&ft=pdf">{file_pdf}</a>&nbsp;';
		}else{
			download_pdf = '';
		}
		if(r.data.file_doc != ''){
			download_doc = '<a href="../paper/download/?id={id}&ft=doc"><img src="../../images/doc.png"></a><a href="../paper/download/?id={id}&ft=doc">{file_doc}</a>';
		}else{
			download_doc = '';
		}
		var paperTplMarkup = [
		'<table>',
			'<tr><td valign="top"><b>' + lbl_title + ':</b></td><td>{title}</td></tr>',
			'<tr><td valign="top"><b>' + lbl_download + ':</b></td><td>' + download_pdf + download_doc + '</td></tr>',
			'<tr><td valign="top"><b>' + lbl_area + ':</b></td><td>{area_name_' + lang + '}</td></tr>',
			'<tr><td valign="top"><b>' + lbl_category + ':</b></td><td>{category_name_' + lang + '}</td></tr>',
			'<tr><td valign="top"><b>' + lbl_abstract + ':</b></td><td>{abstract}</td></tr>',
		'</table>'
		];
		var paperTpl = new Ext.Template(paperTplMarkup);
		paperTpl.overwrite(detailPanel.body, r.data);
	});

    
	ds.load();
		
});
